package com.weige.elefence.utils;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

/**
 * @author ltj
 * @Description 线程池
 * @date 2022/5/26
 */
public class ThreadPoolExecutorUtils {
    private static Logger log = LoggerFactory.getLogger(ThreadPoolExecutorUtils.class);
    private static int corePoolSize = 1;
    private static int maxPoolSize = Integer.MAX_VALUE;
    private static int keepAliveSeconds = 60;
    private static BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue(10);

    private static final ThreadPoolExecutor threadPoolExecutor;

    static {

        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("feather-thread-pool-%d").build();
        threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveSeconds,
                TimeUnit.SECONDS, workQueue, namedThreadFactory);
    }

    public static void run(Runnable command) {
        threadPoolExecutor.execute(command);
    }
}
